package cqrtplm.service.impl;

import com.ty.basic.entity.org.UserDO;
import com.ty.basic.service.snow.SnowflakeIdComponent;
import com.ty.basic.session.TyAccountContext;
import cqrtplm.constant.RTErrorCodeEnum;
import cqrtplm.dto.query.EuEmissionsNOQueryDTO;
import cqrtplm.entity.RtCertFieldEuemissionsnoRelationDO;
import cqrtplm.entity.RtCertFiledDO;
import cqrtplm.mapper.RtCertFieldEuemissionsnoRelationMapper;
import cqrtplm.service.RtCertFieldEuemissionsnoRelationService;
import cqrtplm.service.RtCertFiledService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class RtCertFieldEuemissionsnoRelationServiceImpl implements RtCertFieldEuemissionsnoRelationService {

    @Resource
    private RtCertFieldEuemissionsnoRelationMapper rtCertFieldEuemissionsnoRelationMapper;

    @Resource
    private SnowflakeIdComponent snowflakeIdComponent;

    @Resource
    private RTLogDifferenceServiceImpl rtLogDifferenceService;

    @Resource
    private RtCertFiledServiceImpl rtCertFiledService;

    @Override
    public List<RtCertFieldEuemissionsnoRelationDO> selectRtCertFieldEuemissionsnoRelationList(
            RtCertFieldEuemissionsnoRelationDO rtCertFieldEuemissionsnoRelation) {
        return rtCertFieldEuemissionsnoRelationMapper.selectRtCertFieldEuemissionsnoRelationList(rtCertFieldEuemissionsnoRelation);
    }

    @Override
    public RtCertFieldEuemissionsnoRelationDO selectRtCertFieldEuemissionsnoRelationById(String id) {
        return rtCertFieldEuemissionsnoRelationMapper.selectRtCertFieldEuemissionsnoRelationById(id);
    }

    @Override
    public RtCertFieldEuemissionsnoRelationDO selectRtCertFieldEuemissionsnoRelationByFiledIdAndCustomId(
            String filedId, String customId) {
        return rtCertFieldEuemissionsnoRelationMapper.selectRtCertFieldEuemissionsnoRelationByFiledIdAndCustomId(filedId, customId);
    }

    @Override
    public int insertRtCertFieldEuemissionsnoRelation(RtCertFieldEuemissionsnoRelationDO rtCertFieldEuemissionsnoRelation) {
        String filedId = rtCertFieldEuemissionsnoRelation.getFiledId();
        String customId = rtCertFieldEuemissionsnoRelation.getCustomId();

        RtCertFieldEuemissionsnoRelationDO old = rtCertFieldEuemissionsnoRelationMapper
                .selectRtCertFieldEuemissionsnoRelationByFiledIdAndCustomId(filedId, customId);
        if(old != null) {
            throw RTErrorCodeEnum.DATA_EXISTS.getException("该客户备案机型与欧排号关系已存在");
        }

        UserDO user = TyAccountContext.getUser();
        if (user == null) {
            throw new RuntimeException("用户未登录");
        }

        String snowId = String.valueOf(snowflakeIdComponent.getInstance().nextId());
        rtCertFieldEuemissionsnoRelation.setId(snowId);
        rtCertFieldEuemissionsnoRelation.setCreateUserId(String.valueOf(user.getOid()));
        rtCertFieldEuemissionsnoRelation.setCreateUserName(user.getName());
        rtCertFieldEuemissionsnoRelation.setCreateTime(new Timestamp(System.currentTimeMillis()));
        rtCertFieldEuemissionsnoRelation.setState("草稿");

        rtLogDifferenceService.newLog(
                rtCertFieldEuemissionsnoRelation,null,
                rtCertFieldEuemissionsnoRelation.getId(),
                rtCertFieldEuemissionsnoRelation.getCreateUserName(),
                "表单",
                "新增",
                ""
        );
        return rtCertFieldEuemissionsnoRelationMapper.insertRtCertFieldEuemissionsnoRelation(rtCertFieldEuemissionsnoRelation);
    }

    @Override
    public int updateRtCertFieldEuemissionsnoRelation(RtCertFieldEuemissionsnoRelationDO rtCertFieldEuemissionsnoRelation) {
        RtCertFieldEuemissionsnoRelationDO old = rtCertFieldEuemissionsnoRelationMapper
                .selectRtCertFieldEuemissionsnoRelationById(rtCertFieldEuemissionsnoRelation.getId());
        if(old == null) {
            throw RTErrorCodeEnum.DATA_NOT_EXISTS.getException("该关系记录不存在");
        }

        UserDO user = TyAccountContext.getUser();
        if (user != null) {
            rtCertFieldEuemissionsnoRelation.setUpdateUserId(String.valueOf(user.getOid()));
            rtCertFieldEuemissionsnoRelation.setUpdateUserName(user.getName());
        }
        rtCertFieldEuemissionsnoRelation.setUpdateTime(new Timestamp(System.currentTimeMillis()));

        int r = rtCertFieldEuemissionsnoRelationMapper.updateRtCertFieldEuemissionsnoRelation(rtCertFieldEuemissionsnoRelation);
        if(r > 0) {
            rtLogDifferenceService.newLog(
                    rtCertFieldEuemissionsnoRelation,old,
                    rtCertFieldEuemissionsnoRelation.getId(),
                    rtCertFieldEuemissionsnoRelation.getUpdateUserName(),
                    "表单",
                    "修改",
                    ""
            );
        }
        return r;
    }

    @Override
    public int stopRtCertFieldEuemissionsnoRelation(String id) {
        RtCertFieldEuemissionsnoRelationDO old = rtCertFieldEuemissionsnoRelationMapper.selectRtCertFieldEuemissionsnoRelationById(id);
        if(old == null) {
            throw RTErrorCodeEnum.DATA_NOT_EXISTS.getException("该关系记录不存在");
        }

        UserDO user = TyAccountContext.getUser();
        if (user == null) {
            throw new RuntimeException("用户未登录");
        }

        RtCertFieldEuemissionsnoRelationDO updateDO = new RtCertFieldEuemissionsnoRelationDO();
        updateDO.setId(id);
        updateDO.setState("失效");
        updateDO.setStopUserId(String.valueOf(user.getOid()));
        updateDO.setStopUserName(user.getName());
        updateDO.setStopTime(new Timestamp(System.currentTimeMillis()));
        updateDO.setUpdateUserId(String.valueOf(user.getOid()));
        updateDO.setUpdateUserName(user.getName());
        updateDO.setUpdateTime(new Timestamp(System.currentTimeMillis()));

        return rtCertFieldEuemissionsnoRelationMapper.updateRtCertFieldEuemissionsnoRelation(updateDO);
    }

    @Override
    public int deleteRtCertFieldEuemissionsnoRelationById(String id) {
        return rtCertFieldEuemissionsnoRelationMapper.deleteRtCertFieldEuemissionsnoRelationById(id);
    }

    @Override
    public int deleteRtCertFieldEuemissionsnoRelationByIds(List<String> ids) {
        return rtCertFieldEuemissionsnoRelationMapper.deleteRtCertFieldEuemissionsnoRelationByIds(ids);
    }

    @Override
    public List<RtCertFieldEuemissionsnoRelationDO> queryRtCertFieldEuemissionsnoRelationList(
            EuEmissionsNOQueryDTO entity) {

        return rtCertFieldEuemissionsnoRelationMapper.queryRtCertFieldEuemissionsnoRelationList(entity);
    }
}